package com.xmtlxx.oms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.oms.domain.OperatorLog;
import com.xmtlxx.oms.domain.bo.OperatorLogBo;
import com.xmtlxx.oms.domain.vo.OperatorLogVo;
import com.xmtlxx.oms.mapper.OperatorLogMapper;
import com.xmtlxx.oms.service.IOperatorLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 操作日志Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-10-18
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OperatorLogServiceImpl implements IOperatorLogService {

    private final OperatorLogMapper baseMapper;

    /**
     * 查询操作日志
     *
     * @param id 操作日志主键
     * @return 操作日志
     */
    @Override
    public OperatorLogVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectOperatorLogVoByID(id);
    }

    /**
     * 查询操作日志列表
     *
     * @param bo 操作日志
     * @return 操作日志
     */
    @Override
    public TableDataInfo<OperatorLogVo> queryPageList(OperatorLogBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<OperatorLog> lqw = buildQueryWrapper(bo);
       // Page<OperatorLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<OperatorLogVo> result = baseMapper.selectPageOperatorLogList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询操作日志列表
     *
     * @param bo 操作日志
     * @return 操作日志
     */
    @Override
    public List<OperatorLogVo> queryList(OperatorLogBo bo) {
       // LambdaQueryWrapper<OperatorLog> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectOperatorLogList(bo);
    }

    private LambdaQueryWrapper<OperatorLog> buildQueryWrapper(OperatorLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OperatorLog> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增操作日志
     *
     * @param bo 操作日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(OperatorLogBo bo) {
        OperatorLog add = BeanUtil.toBean(bo, OperatorLog.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OperatorLog insertByEntity(OperatorLogBo bo) {
        OperatorLog add = BeanUtil.toBean(bo, OperatorLog.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return add;
    }

    /**
     * 修改操作日志
     *
     * @param bo 操作日志
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(OperatorLogBo bo) {
        OperatorLog update = BeanUtil.toBean(bo, OperatorLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(OperatorLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除操作日志
     *
     * @param ids 需要删除的操作日志主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
